//
//  AIChatTableView.m
//  Mia
//
//  Created by Vill on 2024/1/23.
//

#import "AIChatTableView.h"
#import "AIInfoFirstTableCell.h"
#import "AIGradientTabView.h"
#import "AIHomeWalletBtn.h"    // 钱包按钮
#import "AIChatManager.h"
#import "AIBalanceAlertView.h"  //金币弹窗
#import "UIApplication+Extension.h"
#import "AITopicTopView.h"

@interface AIChatTableView ()<UITableViewDelegate,UITableViewDataSource,AIChatManagerDelegate>

@property (nonatomic, strong) AIGradientTabView *tableView;   //列表

@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UIButton *closeBtn;
@property (nonatomic, strong) AIHomeWalletBtn *walletBtn;  //钱包按钮
@property (nonatomic, strong) UILabel *nameLbl;
@property (nonatomic, strong) NSMutableArray *messageArr;
@property (nonatomic, assign) CGFloat topicTopHeight;   //顶部话题高度
@property (nonatomic, strong) AITopicTopView *topicTopView; //顶部话题

@end

@implementation AIChatTableView

- (instancetype)initWithFrame:(CGRect)frame{
    if(self == [super initWithFrame:frame]){
        self.backgroundColor = UIColor.clearColor;
        if (AIUIHelperManager.shared.topicChatTop > 0){
            self.topicTopHeight = PTW(50);
            [self addSubview:self.topicTopView];
        }else{
            self.topicTopHeight = 0;
        }
        [self addSubview:self.tableView];
        [self addSubview:self.topView];
        [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.top.trailing.mas_equalTo(self);
            make.height.mas_equalTo(KNavigationBarHeight + PTW(50));
        }];
        // 关闭
        [self addSubview:self.closeBtn];
        [self.closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(PTW(34));
            make.trailing.mas_equalTo(self).mas_offset(-PTW(12.5));
            make.top.mas_equalTo(self).mas_offset(KStatusBarHeight + PTW(5));
        }];
        
        // 钱包
        [self addSubview:self.walletBtn];
        [self.walletBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_greaterThanOrEqualTo(PTW(60));
            make.height.mas_equalTo(PTW(34));
            make.centerY.mas_equalTo(self.closeBtn);
            make.leading.mas_equalTo(self).mas_offset(PTW(12.5));
        }];
        AIUserInfoModel *model = [[AIUserInfoManager shareManager] userInfo];
        [self.walletBtn setWallet:model.coinBalance];
        
        [self.topView setGradientBackgroundWithColors:@[[UIColor colorHex:@"#000000" alpha:0.2],[UIColor colorHex:@"#000000" alpha:0]] locations:@[@0,@1] startPoint:CGPointMake(0,0) endPoint:CGPointMake(0, 1)];
        
        [self.tableView reloadData];
        
        // 背景点击
        [self addTapGestureWithBlock:^(UIGestureRecognizer * _Nonnull gesture) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"keyboardHide" object:nil];
        }];
        [[AIChatManager shareInstance] addDelegate:self delegateQueue:nil];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    if (AIUIHelperManager.shared.topicChatTop > 0){
        self.topicTopView.frame = CGRectMake(0, self.bounds.size.height - self.topicTopHeight, KScreenWidth, self.topicTopHeight);
    }
    self.tableView.frame = CGRectMake(PTW(100), KStatusBarHeight, self.bounds.size.width - PTW(100), self.bounds.size.height - KStatusBarHeight - self.topicTopHeight);
    [self layoutIfNeeded];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AIInfoFirstTableCell *cell = [AIInfoFirstTableCell cellWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return PTW(73);
}

#pragma mark ------------ lazy ---------------
- (AIGradientTabView *)tableView{
    if(!_tableView){
        _tableView = [[AIGradientTabView alloc] initWithFrame:CGRectMake(PTW(100), KStatusBarHeight, self.bounds.size.width - PTW(100), self.bounds.size.height - KStatusBarHeight ) direction:AIGradualDirectionTop gradualValue:@.2f];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = UIColor.clearColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

- (AITopicTopView *)topicTopView{
    if(!_topicTopView){
        _topicTopView = [[AITopicTopView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height, KScreenWidth,0)];
        _topicTopView.topicArr = AIUIHelperManager.shared.topicChatTop;
    }
    return _topicTopView;
}

- (UIView *)topView{
    if(!_topView){
        _topView = [[UIView alloc] init];
    }
    return _topView;
}

- (UIButton *)closeBtn{
    if(!_closeBtn){
        _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_closeBtn setImage:KImaged(@"ai_main_close") forState:UIControlStateNormal];
        [_closeBtn addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeBtn;
}

// 钱包
- (AIHomeWalletBtn *)walletBtn{
    if(!_walletBtn){
        _walletBtn = [AIHomeWalletBtn new];
    }
    return _walletBtn;
}

- (UILabel *)nameLbl{
    if(!_nameLbl){
        _nameLbl = [UILabel new];
        _nameLbl.textColor = UIColor.whiteColor;
        _nameLbl.font = [UIFont fontWithWeight:MTBold size:PTW(20)];
    }
    return _nameLbl;
}

// 关闭
-(void)closeAction{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"keyboardHide" object:nil];
    if (self.closeBlock){
        self.closeBlock();
    }
}

- (NSMutableArray *)messageArr{
    if(!_messageArr){
        _messageArr = [NSMutableArray array];
    }
    return _messageArr;
}

#pragma mark ------------ AIChatManagerDelegate ---------------

- (void)didReceiveMessageWithErrorData:(AIResponse *)response{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"keyboardHide" object:nil];
    if (response.code.integerValue == 6001){
        //需要购买vip弹窗
        
    }else if (response.code.integerValue == 6002){
        //需要购买金币弹窗
        AIBalanceAlertView *balance = [[AIBalanceAlertView alloc] initWithCoins:[AIUserInfoManager shareManager].userInfo.coinBalance subtitle:@"You don't have enough balance"];
        [balance showInView:[[UIApplication sharedExtensionApplication] topWindow]];
    }else if (response.code.integerValue == 6003){
        //亲密度升级弹窗
        
    }
}

- (void)didReceiveMessage:(AIResponse *)response withChatModel:(AIChatResponseContentModel *)chatModel{
    NSLog(@"receive message: ------------- %@ -- %@",chatModel.content,[chatModel mj_JSONObject]);
}


@end
